#!/usr/bin/env ruby 

require "atom/service"

require "rubygems"
require "rexml/document"

class Bet
  @@service = Atom::Service.new "http://littleblackbookdb.cloudapp.net/littleblackbookservice.svc/"
  #@@service = Atom::Service.new "http://localhost:57965/littleblackbookservice.svc/"
  @@name = 'bets'

  @@collection = @@service.collections.find { |c| c.title.to_s == @@name }
  @@feed = @@collection.feed

  attr_accessor :id, :person_id, :event, :value
  attr_accessor :atom_entry, :errors

  def self.find_all
    @@feed.update!

    bets = Array.new
    @@feed.each do |entry|
      bet = Bet.new
      bet.atom_entry = entry
      bet.from_xml( entry.to_xml )
      bets.push( bet )
    end

    return( bets )
  end

  def self.find( id ) 
    return self.find_all.find { |p| p.id == id }
  end

  def self.find_by_person( id ) 
    return self.find_all.select { |p| p.person_id == id }
  end

  def initialize( id = nil, person_id = nil, event = nil, value = nil )
    @id = id
    @person_id = person_id
    @event = event
    @value = value 
    @errors = []
  end

  def from_xml( xml )
    # This is fairly fragile; A parser that understands
    # Microsoft's EDM packaging standard would be far
    # more robust.

    # As it is, this is just using canned XPath queries.

    @id = xml.get_elements( '//d:id' ).first.text
    @person_id = xml.get_elements( '//d:person_id' ).first.text
    @event = xml.get_elements( '//d:event' ).first.text
    @value = xml.get_elements( '//d:value' ).first.text
  end

  def content_to_xml
    doc = REXML::Document.new
    prop = doc.add_element( 'properties' )

    prop.attributes[ 'xmlns:d' ] = "http://schemas.microsoft.com/ado/2007/08/dataservices" 
    prop.attributes[ 'xmlns' ] = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 

    el = prop.add_element( 'd:id' )
    el.attributes[ 'type' ] = 'Edm.Int32'
    el.text = ( @id || 0 ).to_s

    el = prop.add_element( 'd:person_id' )
    el.attributes[ 'type' ] = 'Edm.Int32'
    el.text = ( @person_id || 0 ).to_s

    el = prop.add_element( 'd:event' )
    el.text = @event
 
    el = prop.add_element( 'd:value' )
    el.attributes[ 'type' ] = 'Edm.Double'
    el.text = ( @value || 0 ).to_s

    return doc
  end

  def parse_params( params )
    if params[ :person_id] then
      @person_id = params[ :person_id ]
    end

    if params[ :event ] then
      @event = params[ :event ]
    end

    if params[ :value ] then
      @value = params[ :value ]
    end
  end

  def update_attributes( params )
    parse_params( params )

    @atom_entry.content = content_to_xml
    @atom_entry.content[ 'type' ] = 'application/xml'
    @atom_entry.updated!

    res = @@collection.put! @atom_entry

    @@feed.update!
  end

  def save
    @atom_entry = Atom::Entry.new
    @atom_entry.content = content_to_xml
    @atom_entry.content[ 'type' ] = 'application/xml'
    @atom_entry.updated!

 
    res = @@collection.post! @atom_entry

    @@feed.update!
    @atom_entry = @@feed.entries.last
    from_xml @atom_entry.content.to_xml 
  end

  def destroy
    res = @@collection.delete! @atom_entry
  end

  def to_s 
    return "#{@id}"
  end

  def person
    return Person.find( @person_id )
  end

end

